/**
 * 
 */
package com.linrung.sf.scheduler;

import com.linrung.sf.scheduler.zwwx.jms.ZWWXMsgConsumerService;
import com.linrung.sf.scheduler.zwwx.jms.ZWWXMsgProductorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

/**
 * @author 袁彬
 *
 */
@Configuration
@ImportResource(value={"classpath:/spring/applicationContext-timer.xml","classpath:/spring/applicationContext-ActiveMQ.xml",})
public class SpringSFTaskConfig {

	@Autowired
	private CachingConnectionFactory connectionFactory;
	@Autowired
	private ZWWXMsgConsumerService zwwxMsgConsumerService;

	@Bean("jmsListenerContainerQueue")
	public JmsListenerContainerFactory jmsListenerContainerFactory() {
		DefaultJmsListenerContainerFactory defaultMessageListenerContainer = new DefaultJmsListenerContainerFactory();
		defaultMessageListenerContainer.setConnectionFactory(connectionFactory);
		defaultMessageListenerContainer.setPubSubDomain(false);
		defaultMessageListenerContainer.setSubscriptionDurable(false);
		defaultMessageListenerContainer.setSubscriptionShared(false);
//		defaultMessageListenerContainer.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
		defaultMessageListenerContainer.setSessionTransacted(true);
		defaultMessageListenerContainer.setReceiveTimeout((long) (1000*60*5));
		return defaultMessageListenerContainer;
	}
	
	@Bean
	public DefaultMessageListenerContainer defaultSendZWWXMessageListenerContainer() {
		DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
		defaultMessageListenerContainer.setDestinationName(ZWWXMsgProductorService.QUEUE_NAME);
		defaultMessageListenerContainer.setConnectionFactory(connectionFactory);
		defaultMessageListenerContainer.setPubSubDomain(false);
		defaultMessageListenerContainer.setSubscriptionDurable(false);
		defaultMessageListenerContainer.setSubscriptionShared(false);
		defaultMessageListenerContainer.setMessageListener(zwwxMsgConsumerService);
		defaultMessageListenerContainer.setConcurrentConsumers(3);
//		defaultMessageListenerContainer.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);
		defaultMessageListenerContainer.setSessionTransacted(true);
		defaultMessageListenerContainer.setReceiveTimeout(1000*60*5);
		
		return defaultMessageListenerContainer;
	}

}
